package com.yiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiyi.entity.ExamQuestion;
import com.yiyi.mapper.ExamQuestionMapper;
import com.yiyi.service.ExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yiyi
 * @date 2021/5/31
 */
@Service
public class ExamQuestionServiceImpl implements ExamQuestionService {

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 根据试卷id获取试卷包含题目详细信息
     *
     * @param examId
     * @param questionType
     * @return
     * */
    @Override
    public List<String> findByExamId(String examId, int questionType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("exam_id", examId);
        queryWrapper.eq("question_type", questionType);
        List<ExamQuestion> examQuestions = examQuestionMapper.selectList(queryWrapper);
        List<String> questionIds = new ArrayList<>();
        for (ExamQuestion examQuestion : examQuestions) {
            questionIds.add(examQuestion.getQuestionId());
        }
        return questionIds;
    }

    /**
     * 新增试卷包含题目
     *
     * @param examId
     * @param questionType
     * @param ids
     * */
    @Override
    public void saveNewExam(String examId, int questionType, List<String> ids) {
        ExamQuestion examQuestion = new ExamQuestion();
        // 对列表去重
        List<String> list = (List<String>) ids.stream().distinct().collect(Collectors.toList());
        for (String id : list) {
            examQuestion.setExamId(examId);
            examQuestion.setQuestionId(id);
            examQuestion.setQuestionType(questionType);
            examQuestionMapper.insert(examQuestion);
        }
    }

    /**
     * 根据试卷id删除对应的试卷
     *
     * @param examId
     * @return
     * */
    @Override
    public int deleteByExamId(String examId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("exam_id", examId);
        return examQuestionMapper.delete(queryWrapper);
    }
}
